import axios from 'axios'
import store from '@/store'
import {
  token_expires,count_expires
} from './Keyword'
import {
  clearKey,clearDynamicKey,counter
} from './responseScreen'
const host = ""

const service = axios.create({
  baseURL: host,
  timout: 10000
})
service.interceptors.request.use(config => {

  if (store.getters.dynamicCode.JWT) {
    config.headers['YF-JWT'] = store.getters.dynamicCode.JWT;
  } else if (store.getters.key) {
    config.headers['YF-JWT'] = store.getters.key.JWT;
  }
  config.headers['PF'] = "web";
  config.headers['YF-BID'] = "yw";
  config.headers['YF-VER'] = "2.1.0"
  if (config.method === 'post' || config.method === 'put' || config.method === 'delete') {
    config.data = config.params
    config.params = null;
  }

  return config;
}, error => {
  console.log(error)
})

service.interceptors.response.use(res => {
  var respones = res.data;
  // this.$vux.toast.show({
  //   text: 'toast'
  // })
  if (res.data.code == "5102"||res.data.code == "5103"||res.data.code == "5104") {
    clearKey()
    clearDynamicKey()
    if(counter(res)){
      return count_expires;
    }
    return token_expires;
  }
  if(res.data.code=="9999"){ 
     return false;
  }
  
  if(res.data.code=="5000"||res.data.code=="404"){
    store.dispatch("SHOW_TOAST", true)
  }
  if (respones && respones.data) {
    return respones.data
  }
  return res;

}, error => {
  store.dispatch("SHOW_TOAST", true)
  console.log(error)
})
export default service
